package xin.zhiyin.控制.system.电子书;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.zhiyin.实体.BaseController;
import xin.zhiyin.实体.QueryRequest;
import xin.zhiyin.实体.书Entity;
import xin.zhiyin.工具.ResponseBo;
import xin.zhiyin.服务.书.书Service;

import java.util.*;

@Controller
@RequestMapping(path = "/system/ebook/book")//页面的url地址
public class 书Controller  extends BaseController {
    @Autowired
    书Service 书Service;

    @GetMapping(path = "/list")
    public String 类目列表(Long categoryId,Model model)
    {
        model.addAttribute("categoryId",categoryId);
        return "system/ebook/book/list";
    }
    @RequestMapping("/add")
    @ResponseBody
    public ResponseBo 添加书籍(书Entity book , String categoryIds)
    {
        if(StringUtils.isBlank(book.getTitle())) {
            return ResponseBo.error("请输入书标题");
        }
        书Service.保存(book);
        return ResponseBo.ok("新增文章成功");
    }

    @PostMapping("/update")
    @ResponseBody
    public ResponseBo updateBook(书Entity 书) {
        书Service.更新(书);
        return ResponseBo.ok("更新书成功");
    }
    @RequestMapping("/listData")
    @ResponseBody
    public Map<String, Object> 书分页数据列表(QueryRequest request, Long categoryId) {
        Map<String, Object> postMap = new HashMap<>();
        if( categoryId == null ){
            postMap = this.selectByPageNumSize(request, () -> 书Service.获取所有书());
        }else {
            postMap = this.selectByPageNumSize(request, () -> 书Service.根据categoryId获取书(categoryId));
        }
        return postMap;
    }
    @RequestMapping("/getBookById")
    @ResponseBody
    public ResponseBo getEBook(Long bookId) {
        书Entity 书 = 书Service.根据Id获取书(bookId);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("metas", null);
        data.put("taxids", null);
        data.put("tags", null);
        data.put("book",书);
        return ResponseBo.ok(data);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseBo deleteEBook(String bookIds) {
        List<String> ids = Arrays.asList(bookIds.split(","));
        List<Long> LString = new ArrayList();
        for(String st:ids)
        {
            LString.add(Long.valueOf(st));
        }
        书Service.批量删除(LString);

        return ResponseBo.ok(LString);
    }
}
